import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

import postCssPxToRem from 'postcss-pxtorem'
import autoprefixer from 'autoprefixer';

// 引入svg插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

const pathResolve = (pathStr) => {
  return path.resolve(__dirname, pathStr)
}


// https://vitejs.dev/config/
export default defineConfig({
  base: process.env.NODE_ENV == 'development' ? '' : '/fitasty/h5',
  plugins: [vue(), vueJsx(),
  createSvgIconsPlugin({
    // 指定需要缓存的svg图标文件夹，即需要识别的svg都应该放在这个文件夹下
    iconDirs: [pathResolve('./src/assets/icons')],
    symbolId: 'icon-[dir]-[name]',
  }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    host: '192.168.10.98',
    open: true,
    port: 3000,
    hmr: true,
    https: false,
    proxy: {
      '/fitasty': {
        target: 'http://47.107.64.214',
        // target: 'https://7t039348q9.vicp.fun',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/fitasty/, '/fitasty')
      }
    }
  },
  /* build: {
     minify: 'terser',
     terserOptions: {
       compress: {
         drop_console: true,
         drop_debugger: true
       }
     }
   },*/
  css: {
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: ['Android 4.1', 'iOS 7.1', 'Chrome > 31', 'ff > 31', 'ie >= 8'],
        }),
        postCssPxToRem({
          // 自适应，px>rem转换
          rootValue: 37.5, // 75表示750设计稿，37.5表示375设计稿
          propList: ['*'], // 需要转换的属性，这里选择全部都进行转换
          selectorBlackList: ['norem'], // 过滤掉norem-开头的class，不进行rem转换
        }),
      ]
    },
    preprocessorOptions: {
      scss: {
        // 配置全局sass变量注入
        // additionalData: '@import "@/styles/variable.scss";'
      }
    }
  }
})

